package org.jeecg.modules.paycenter.channel.zzbank.service.impl;

import cn.hutool.http.HttpRequest;
import org.jeecg.modules.paycenter.channel.zzbank.service.IHyZhengzhouBankService;
import org.jeecg.modules.paycenter.channel.zzbank.util.SnowFlakeUtil;
import org.jeecg.modules.paycenter.channel.zzbank.util.ZhengzhouBankUtil;
import org.jeecg.modules.paycenter.channel.zzbank.vo.AllocateVo;
import org.jeecg.modules.paycenter.channel.zzbank.vo.WithdrawalVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class HyZhengzhouBankServiceImpl implements IHyZhengzhouBankService {
    @Value(value="${zzBank.serverUrl:}")
    private String zzBankUrl;

    @Value(value="${zzBank.accountName:}")
    private String accountName;

    @Value(value="${zzBank.accountNo:}")
    private String accountNo;

    @Value(value="${zzBank.payPassword:}")
    private String payPassword;

    private ZhengzhouBankUtil zhengzhouBankUtil = new ZhengzhouBankUtil();


    /**
     * @param acNo
     * @param bankCode
     * @return
     * @throws Exception
     */
    @Override
    public Document BindIncomingPayment(String acNo, String bankCode) throws Exception {
        String erpJnlNo = String.valueOf(SnowFlakeUtil.nextId());
        String urlPath = zzBankUrl + "/eb2e/B2EVirtualAccountBindPayer.do?userPassword=" + payPassword+ "&SIGDATA=0";
        StringBuffer xDataBody = new StringBuffer();
        xDataBody.append("<QryOrBind>1</QryOrBind>");
        xDataBody.append("<NomAccNo>" + acNo + "</NomAccNo>");
        xDataBody.append("<List>");
        xDataBody.append("<Map><PayAcNo>" + bankCode + "</PayAcNo></Map>");
        xDataBody.append("</List>");
        Document document = this.zhengzhouBankUtil.zzbankPayment(urlPath, "B2EVirtualAccountBindPayer", erpJnlNo, xDataBody.toString());
        return document;
    }

    /**
     * @param acNo
     * @return
     * @throws Exception
     */
    @Override
    public Document cancellationBankAccount(String acNo) throws Exception {
        String erpJnlNo = String.valueOf(SnowFlakeUtil.nextId());
        String urlPath = zzBankUrl + "/eb2e/B2EVaccountClose.do?userPassword=" + payPassword+ "&SIGDATA=0";
        StringBuffer xDataBody = new StringBuffer();
        xDataBody.append("<ParentRAcNo>" + accountNo + "</ParentRAcNo>");
        xDataBody.append("<ParentRAcName>" + accountName + "</ParentRAcName>");
        xDataBody.append("<AcNo>" + acNo + "</AcNo>");
        Document document = this.zhengzhouBankUtil.zzbankPayment(urlPath, "B2EVaccountClose", erpJnlNo, xDataBody.toString());
        return document;
    }

    /**
     * @param userName
     * @return
     */
    @Override
    public Document createNoerrorBankAccount(String userName) {
        String erpJnlNo = String.valueOf(SnowFlakeUtil.nextId());
        String urlPath = zzBankUrl + "/eb2e/B2EVaccountOpen.do?userPassword=" + payPassword+ "&SIGDATA=0";
        StringBuffer xDataBody = new StringBuffer();
        xDataBody.append("<ParentRAcNo>" + accountNo + "</ParentRAcNo>");
        xDataBody.append("<ParentRAcName>" + accountName + "</ParentRAcName>");
        xDataBody.append("<UpAcNo>" + accountNo + "</UpAcNo>");
        xDataBody.append("<AcName>" + userName + "</AcName>");
        xDataBody.append("<DummyRule>3</DummyRule>");
        xDataBody.append("<AccrualFlag>1</AccrualFlag>");
        xDataBody.append("<NameFlag>1</NameFlag>");
        Document document = this.zhengzhouBankUtil.zzNoerrorsbankPayment(urlPath, "B2EVaccountOpen", erpJnlNo, xDataBody.toString());
        return document;
    }

    /**
     * @param userName
     * @return
     * @throws Exception
     */
    @Override
    public Document createBankAccount(String userName) throws Exception {
        String erpJnlNo = String.valueOf(SnowFlakeUtil.nextId());
        String urlPath = zzBankUrl + "/eb2e/B2EVaccountOpen.do?userPassword=" + payPassword+ "&SIGDATA=0";
        StringBuffer xDataBody = new StringBuffer();
        xDataBody.append("<ParentRAcNo>" + accountNo + "</ParentRAcNo>");
        xDataBody.append("<ParentRAcName>" + accountName + "</ParentRAcName>");
        xDataBody.append("<UpAcNo>" + accountNo + "</UpAcNo>");
        xDataBody.append("<AcName>" + userName + "</AcName>");
        xDataBody.append("<DummyRule>3</DummyRule>");
        xDataBody.append("<AccrualFlag>1</AccrualFlag>");
        xDataBody.append("<NameFlag>1</NameFlag>");
        Document document = this.zhengzhouBankUtil.zzbankPayment(urlPath, "B2EVaccountOpen", erpJnlNo, xDataBody.toString());
        return document;
    }

    /**
     * 虚户之间的划拨
     * @return
     */
    @Override
    public Document allocateTransfer(AllocateVo allocateVo) throws Exception {
        String urlPath = zzBankUrl + "/eb2e/B2EVFundAllotTransfer.do?userPassword=" + payPassword+ "&SIGDATA=1";
        StringBuffer xDataBody = new StringBuffer();
        xDataBody.append("<PayAcNo>" + allocateVo.getPayAcNo() + "</PayAcNo>");
        xDataBody.append("<CollectAcNo>" + allocateVo.getPutAcNo() + "</CollectAcNo>");
        xDataBody.append("<TransAmt>" + allocateVo.getTransAmt() + "</TransAmt>");
        xDataBody.append("<ParentRAcNo>" + accountNo + "</ParentRAcNo>");
        xDataBody.append("<ParentRAcName>" + accountName + "</ParentRAcName>");
        xDataBody.append("<PostScript>" + accountName + "</PostScript>");
        Document document = this.zhengzhouBankUtil.zzbankPayment(urlPath, "B2EVFundAllotTransfer", allocateVo.getErpJnlNo(), xDataBody.toString());
        return document;
    }

    /**
     * 公司提现
     *
     * @param withdrawalVo@return
     */
    @Override
    public Document companyWithdrawal(WithdrawalVo withdrawalVo) throws Exception {
        String payTyoe = "B2EVBankInnerTransfer";
        if (!withdrawalVo.getPayeeBankName().startsWith("郑州")) {
            payTyoe = "B2EVCrossBankTransfer";
        }

        String erpJnlNo = String.valueOf(SnowFlakeUtil.nextId());
        String urlPath = zzBankUrl + "/eb2e/" + payTyoe + ".do?userPassword=" + payPassword+ "&SIGDATA=1";
        StringBuffer xDataBody = new StringBuffer();
        xDataBody.append("<PayerAcNo>" + withdrawalVo.getPayerAcNo() + "</PayerAcNo>");
        xDataBody.append("<PayerAcName>" + withdrawalVo.getPayerAcName() + "</PayerAcName>");
        xDataBody.append("<PayeeAcNo>" + withdrawalVo.getPayeeAcNo() + "</PayeeAcNo>");
        xDataBody.append("<PayeeAcName>" + withdrawalVo.getPayeeAcName() + "</PayeeAcName>");
        xDataBody.append("<ParentRAcNo>" + accountNo + "</ParentRAcNo>");
        xDataBody.append("<ParentRAcName>" + accountName + "</ParentRAcName>");
        if ("B2EVBankInnerTransfer".equals(payTyoe)) {
            xDataBody.append("<TransAmt>" + withdrawalVo.getTransAmt() + "</TransAmt>");
        } else {
            xDataBody.append("<BankCode>" + withdrawalVo.getBankCode() + "</BankCode>");
            xDataBody.append("<PayeeBankName>" + withdrawalVo.getPayeeBankName() + "</PayeeBankName>");
            xDataBody.append("<PayeeBankId>" + withdrawalVo.getBankCode() + "</PayeeBankId>");
            xDataBody.append("<PayeeDeptName>" + withdrawalVo.getPayeeBankName() + "</PayeeDeptName>");
            xDataBody.append("<PayeeDeptId>" + withdrawalVo.getBankCode() + "</PayeeDeptId>");
            xDataBody.append("<TrsAmount>" + withdrawalVo.getTransAmt() + "</TrsAmount>");
            xDataBody.append("<TransferType>N</TransferType>");
        }

        xDataBody.append("<Remark>运费提现</Remark>");
        Document document = this.zhengzhouBankUtil.zzbankPayment(urlPath, payTyoe, erpJnlNo, xDataBody.toString());
        String processState = document.getElementsByTagName("ProcessState").item(0).getFirstChild().getNodeValue();
        return document;
    }


    @Override
    public Document withdrawal(WithdrawalVo withdrawalVo) throws Exception {
        String payTyoe = "B2EVBankInnerTransfer";
        if (!withdrawalVo.getPayeeBankName().startsWith("郑州")) {
            payTyoe = "B2EVCrossBankTransfer";
        }

        String urlPath = zzBankUrl + "/eb2e/" + payTyoe + ".do?userPassword=" + payPassword+ "&SIGDATA=0";
        StringBuffer xDataBody = new StringBuffer();
        xDataBody.append("<PayerAcNo>" + withdrawalVo.getPayerAcNo() + "</PayerAcNo>");
        xDataBody.append("<PayerAcName>" + withdrawalVo.getPayerAcName() + "</PayerAcName>");
        xDataBody.append("<PayeeAcNo>" + withdrawalVo.getPayeeAcNo() + "</PayeeAcNo>");
        xDataBody.append("<PayeeAcName>" + withdrawalVo.getPayeeAcName() + "</PayeeAcName>");
        xDataBody.append("<ParentRAcNo>" + accountNo + "</ParentRAcNo>");
        xDataBody.append("<ParentRAcName>" + accountName + "</ParentRAcName>");
        if ("B2EVBankInnerTransfer".equals(payTyoe)) {
            xDataBody.append("<TransAmt>" + withdrawalVo.getTransAmt() + "</TransAmt>");
        } else {
            xDataBody.append("<BankCode>" + withdrawalVo.getBankCode() + "</BankCode>");
            xDataBody.append("<PayeeBankName>" + withdrawalVo.getPayeeBankName() + "</PayeeBankName>");
            xDataBody.append("<PayeeBankId>" + withdrawalVo.getBankCode() + "</PayeeBankId>");
            xDataBody.append("<PayeeDeptName>" + withdrawalVo.getPayeeBankName() + "</PayeeDeptName>");
            xDataBody.append("<PayeeDeptId>" + withdrawalVo.getBankCode() + "</PayeeDeptId>");
            xDataBody.append("<TrsAmount>" + withdrawalVo.getTransAmt() + "</TrsAmount>");
            xDataBody.append("<TransferType>N</TransferType>");
        }

        xDataBody.append("<Remark>" + withdrawalVo.getRemark() + "</Remark>");

        Document document = this.zhengzhouBankUtil.zzbankPayment(urlPath, payTyoe, withdrawalVo.getErpJnlNo(), xDataBody.toString());
        String processState = document.getElementsByTagName("ProcessState").item(0).getFirstChild().getNodeValue();
        String transJnl = document.getElementsByTagName("TransJnl").item(0).getFirstChild().getNodeValue();
        return document;
    }

    /**
     * @param acNo
     * @param transType
     * @return
     */
    @Override
    public Document queryPaymentResult(String erpJnlNo, String transJnl,String acNo, String transType, String startDate, String endDate) {
        //String erpJnlNo = String.valueOf(SnowFlakeUtil.nextId());
        //TODO 上线需要修改这里！！！！！！！！！！！！
        //String startStr = "2024-11-01";
        String urlPath = zzBankUrl + "/eb2e/B2EVAccountTrsJnlQry.do?userPassword=" + payPassword+ "&SIGDATA=1";
        StringBuffer xDataBody = new StringBuffer();
        xDataBody.append("<AcNo>" + acNo + "</AcNo>");
        xDataBody.append("<TransType>" + transType + "</TransType>");
        xDataBody.append("<TransJnl>" +  transJnl+ "</TransJnl>");
        xDataBody.append("<StartDate>"+startDate+"</StartDate>");
        xDataBody.append("<EndDate>"+endDate+"</EndDate>");
        xDataBody.append("<StartIndex>0</StartIndex>");
        xDataBody.append("<ItemsCount>50</ItemsCount>");
        xDataBody.append("<ErpJnlNo>" + erpJnlNo + "</ErpJnlNo>");
        return this.zhengzhouBankUtil.zzNoerrorsbankPayment(urlPath, "B2EVAccountTrsJnlQry", erpJnlNo, xDataBody.toString());
    }

    /**
     * @param createTime
     * @param acNo
     * @param transType
     * @return
     */
    @Override
    public Document queryTransferAccounts(Date createTime, String acNo, String transType) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String erpJnlNo = String.valueOf(SnowFlakeUtil.nextId());
        String urlPath = zzBankUrl + "/eb2e/B2EVAccountTrsJnlQry.do?userPassword=" + payPassword+ "&SIGDATA=0";
        StringBuffer xDataBody = new StringBuffer();
        xDataBody.append("<AcNo>" + acNo + "</AcNo>");
        xDataBody.append("<TransType>" + transType + "</TransType>");
        xDataBody.append("<StartDate>" + sdf.format(createTime) + "</StartDate>");
        xDataBody.append("<EndDate>" + sdf.format(new Date()) + "</EndDate>");
        xDataBody.append("<StartIndex>0</StartIndex>");
        xDataBody.append("<ItemsCount>50</ItemsCount>");
        return this.zhengzhouBankUtil.zzNoerrorsbankPayment(urlPath, "B2EVAccountTrsJnlQry", erpJnlNo, xDataBody.toString());
    }


    /**
     * 查询清分账户信息
     *
     * @param subAccNo
     * @return
     */
    @Override
    public Document querySubAccInfo(String subAccNo) {
        String erpJnlNo = String.valueOf(SnowFlakeUtil.nextId());
        String urlPath = zzBankUrl + "/eb2e/B2EVaccountQry.do?userPassword=" + payPassword+ "&SIGDATA=0&_local=zh_CN";
        StringBuffer xDataBody = new StringBuffer();
        xDataBody.append("<AcNo>" + subAccNo + "</AcNo>");
        xDataBody.append("<QueryRange>" + 1 + "</QueryRange>");
        xDataBody.append("<StartIndex>0</StartIndex>");
        xDataBody.append("<ItemsCount>10</ItemsCount>");
        return this.zhengzhouBankUtil.zzNoerrorsbankPayment(urlPath, "B2EVaccountQry", erpJnlNo, xDataBody.toString());
    }

    /**
     * @param transJnl
     * @param DealJnl
     * @return
     */
    @Override
    public Document queryReceipt(String transJnl, String DealJnl) {
        String erpJnlNo = String.valueOf(SnowFlakeUtil.nextId());
        String urlPath = zzBankUrl + "/eb2e/B2EVDigitQry.do?userPassword=" + payPassword+ "&SIGDATA=1";
        StringBuffer xDataBody = new StringBuffer();
        xDataBody.append("<TransJnl>" + transJnl + "</TransJnl>");
        xDataBody.append("<DealJnl>" + DealJnl + "</DealJnl>");
        return this.zhengzhouBankUtil.zzNoerrorsbankPayment(urlPath, "B2EVDigitQry", erpJnlNo, xDataBody.toString());
    }

    /**
     * @param fileName
     * @param transCode
     * @return
     */
    @Override
    public String downReceipt(String fileName, String transCode) throws Exception {
        String urlPath = zzBankUrl + "/eb2e/downloadFile.do?userPassword=" + payPassword+ "&SIGDATA=1&fileName="+fileName+"&transCode=B2EVDigitQry|"+transCode;
        return this.zhengzhouBankUtil.zzFilebankPayment(urlPath).toString();
    }
}
